package com.ysd.crm.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.crm.entity.UserRoles;
import com.ysd.crm.entity.Users;
import com.ysd.crm.entity.seach.UsersSearch;
import com.ysd.crm.mapper.UsersMapper;
import com.ysd.crm.service.UsersService;
import com.ysd.crm.utils.LayResult;
import com.ysd.crm.utils.MD5Util;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/users")
@Api
@RestController
public class UsersController {
	@Autowired
	private UsersMapper uMapper;
	
	@Autowired//ytt
	private UsersService usersService; 
	/**
	 * 多条件查询
	 * @param u
	 * @return
	 */
	@ApiOperation("多条件查询-gmw")
	@GetMapping("getUsers")
	public Object getUsers(UsersSearch u) {
		u.setPage((u.getPage()-1)*u.getLimit());
		List<Users> n= uMapper.getUsers(u);
		if(n!=null) {
			int count=uMapper.getCount(u);
			return LayResult.toClient("0", "查询成功", count, n);
		}
		return LayResult.toClient("1", "查询失败");
	}
	/**
	 * 添加数据
	 * @param s
	 * http://localhost:8080/users/getUsers?limit=10&page=1
	 * @return
	 */
	@ApiOperation("添加数据-gmw")
	@PostMapping("addUsers")
	public Object addUsers(Users s) {
		
		int u=uMapper.getName(s);					
		if(u==0) {
			//添加密码MD5加密			
			s.setPassword(MD5Util.getMD5("ysd123"));
			s.setCreateTime(new Date());
			int n=uMapper.addUsers(s);
			if(n>0) {
				return LayResult.toClient("0", "添加成功");
			}
			return LayResult.toClient("1", "添加失败");
		}else {
			
			return LayResult.toClient("1", "该用户已存在");
		}
		
	}
	
	/**
	 * ytt
	 * 是否锁定
	 * @param u
	 * @return
	 */
	@ApiOperation("解除锁定-ytt")
	@PostMapping("isLockout")
	public Object isLockout(UsersSearch u) {
		Users user = usersService.findByLockout(u);
		if(user.getIsLockout().equals(u.getSuoDing())) {
				return LayResult.toClient("0", "已解除");
		}else {
			u.setIsLockout("否");
			int n = usersService.updateLockout(u);
			if(n>0) {
				return LayResult.toClient("1", "解除锁定");
			}else {
				return LayResult.toClient("2", "解除锁定失败");
			}
		}
	}
	/**
	 * ytt
	 * 是否锁定
	 * @param u
	 * @return
	 */
	@ApiOperation("锁定-ytt")
	@PostMapping("isNoLockout")
	public Object isNoLockout(UsersSearch u) {
		Users user = usersService.findByLockout(u);
		if(user.getIsLockout().equals(u.getSuoDing())) {
			return LayResult.toClient("0", "已锁定");
		}else {
			u.setIsLockout("是");
			int n = usersService.updateLockout(u);
			if(n>0) {
				return LayResult.toClient("1", "锁定成功");
			}else {
				return LayResult.toClient("2", "锁定失败");
			}
				
		}
	}
	/**
	 * ytt
	 * 重置密码
	 * @param u
	 * @return
	 */
	@ApiOperation("修改-ytt")
	@PostMapping("updUsers")
	public Object updUsers(Users u) {
		u.setPsdWrongTime(0);
		u.setIsLockout("否");
		int n = usersService.updateUsers(u);
		if(n>0) {
			return LayResult.toClient("0", "修改成功");
		}
		return LayResult.toClient("1", "修改失败");
	}
	/**
	 * ytt
	 * 重置密码
	 * @param u
	 * @return
	 */
	@ApiOperation("重置密码-ytt")
	@PostMapping("chongZhiMiMa")
	public Object updPas(Users u) {
		u.setPassword(MD5Util.getMD5("ysd123"));
		int n = usersService.updatePas(u);
		if(n>0) {
			return LayResult.toClient("0", "重置成功");
		}
		return LayResult.toClient("1", "重置失败");
	}
	
	/**
	 * ytt
	 * 删除 Users
	 * @param u
	 * @return
	 */
	@ApiOperation("删除-ytt")
	@PostMapping("delUsers")
	public Object delUsers(String Id) {
		List<UserRoles> ur = usersService.findById(Id);
		
		if(ur.size() == 0) {
			int n = usersService.deleteUsers(Id);
			if(n>0) {
				return LayResult.toClient("1", "删除成功");
			}
			return LayResult.toClient("2", "删除失败");
		}else {
			return LayResult.toClient("0", "用户有角色，无法删除");
		}
		
	}
	
	/**
	 * ytt
	 * 删除 Users
	 * @param u
	 * @return
	 */
	@ApiOperation("修改密码-ytt")
	@PostMapping("updUserPasById")
	public Object updUserPasById(String Id,String newPas, String twoPas,String oldPas) {
		Users u = new Users();
		u.setId(Id);
		Users pwd = usersService.findUserPasById(u);
		System.out.println(pwd.getPassword());
		System.out.println(MD5Util.getMD5(oldPas));
		if(pwd.getPassword().equals(MD5Util.getMD5(oldPas))) {
			if(newPas.equals(twoPas)) {
				u.setPassword(MD5Util.getMD5(newPas));
				int n = usersService.updateUserPasById(u);
				if(n>0) {
					return LayResult.toClient("0", "修改密码成功");
				}
				return LayResult.toClient("1", "修改密码失败");
			}
			return LayResult.toClient("2", "两次新密码不同");
		}
			return LayResult.toClient("3", "原密码错误");
		
	}
}
